package com.vrp3d.common.interceptor;

import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.servlet.config.annotation.InterceptorRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;

/**
 * 配置拦截器生效
 *
 * @author vrp3d
 */
@Configuration
public class WebConfiguration implements WebMvcConfigurer {

    /**
     * 这里加上注解@Bean新建一个拦截器注入到bean中,在拦截器中注入service才会有用,否者拦截器中引入的服务都是null
     *
     * @return
     */
    @Bean
    public MyInterceptor myInterceptor() {
        return new MyInterceptor();
    }

    /**
     * 配置拦截路径,把拦截器注入到bean中
     *
     * @param registry
     */
    @Override
    public void addInterceptors(InterceptorRegistry registry) {
        registry.addInterceptor(myInterceptor()).addPathPatterns("/**");
    }
}
